﻿using System.Drawing;
using System.IO;
using ElectronicCommerce.Framework.Web.UI.GeneratedImage.ImageTransform;

namespace ElectronicCommerce.Framework.Web.UI.GeneratedImage
{
    /// <summary>
    /// 
    /// </summary>
    public class ImageGenerateRequest
    {
        public ImageGenerateRequest(string imagePath, int width, int height)
        {
            this.OriginalWidth = 0;
            this.OriginalHeight = 0;
            this.RequestWidth = width;
            this.RequestHeight = height;
            this.FactWidth = width;
            this.FactHeight = height;
            this.ResizeMode = ImageResizeMode.Fit;

            this.ImagePath = imagePath;
            using (Image originalImage = Image.FromFile(this.ImagePath))
            {
                if (originalImage != null)
                {
                    this.OriginalWidth = originalImage.Width;
                    this.OriginalHeight = originalImage.Height;
                }
            }

            //if (this.OriginalWidth == 0 || this.OriginalHeight == 0)
            //    throw new InvalidOperationException("Original image format invalid.");

            //if (this.RequestWidth <= 0 || this.RequestWidth > this.OriginalWidth)
            //    this.FactWidth = this.OriginalWidth;

            //if (this.RequestHeight <= 0 || this.RequestHeight > this.OriginalHeight)
            //    this.FactHeight = this.OriginalHeight;
        }

        /// <summary>
        /// 原始宽度
        /// </summary>
        public int OriginalWidth { get; set; }

        /// <summary>
        /// 原始高度
        /// </summary>
        public int OriginalHeight { get; set; }

        /// <summary>
        /// 请求宽度
        /// </summary>
        public int RequestWidth { get; set; }

        /// <summary>
        /// 请求高度
        /// </summary>
        public int RequestHeight { get; set; }

        /// <summary>
        /// 实际请求宽度
        /// </summary>
        public int FactWidth { get; set; }

        /// <summary>
        /// 实际请求高度
        /// </summary>
        public int FactHeight { get; set; }

        /// <summary>
        /// 图片转换模式
        /// </summary>
        public ImageResizeMode ResizeMode { get; set; }

        /// <summary>
        /// 原始文件的绝对路径
        /// </summary>
        public string ImagePath { get; set; }

        private string _ThumbnailImagePath = "";
        /// <summary>
        /// 缩略图文件的绝对路径
        /// </summary>
        public string ThumbnailImagePath
        {
            get
            {
                string thumbnailImagePath = _ThumbnailImagePath;
                if (thumbnailImagePath == "")
                {
                    string imagePathOnly = Path.GetDirectoryName(this.ImagePath);
                    string imageFileName = Path.GetFileName(this.ImagePath);
                    if (this.RequestWidth > 0 && this.RequestHeight > 0)
                        thumbnailImagePath = string.Format("{0}\\W{1}H{2}\\{3}", imagePathOnly, this.RequestWidth, this.RequestHeight, imageFileName);
                    else if (this.RequestWidth > 0 && this.RequestHeight <= 0)
                        thumbnailImagePath = string.Format("{0}\\W{1}\\{2}", imagePathOnly, this.RequestWidth, imageFileName);
                    else if (this.RequestWidth <= 0 && this.RequestHeight > 0)
                        thumbnailImagePath = string.Format("{0}\\H{1}\\{2}", imagePathOnly, this.RequestHeight, imageFileName);
                }

                return thumbnailImagePath;
            }
            set
            {
                _ThumbnailImagePath = value;
            }
        }

        /// <summary>
        /// 默认文件的路径
        /// 当原始文件不存在的情况使用
        /// </summary>
        public string DefaultImagePath { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public bool IsRequestOriginalImage
        {
            get
            {
                return this.OriginalWidth == this.FactWidth && this.OriginalHeight == this.FactHeight;
            }
        }
    }
}
